<template>
  <div class="app-container">
    <div class="row row-border-top">
      <div class="cell" style="flex: 1; text-align: center;font-size: 24px;">
        <b>闲置设备/车辆出租</b>
      </div>
      <i style="color: #C03639" >温馨提示：如需将自己的设备出租，请将设备修改为闲置!</i>
    </div>
    <el-divider></el-divider>
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:vehicle:add']"
        >我要发布</el-button>
      </el-col>

      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        class="el-form-search"
      >
        <el-form-item label="设备名称" prop="name" class="el-form-search-item">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入设备名称"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="plateNumber" class="el-form-search-item">
          <el-input
            v-model="queryParams.plateNumber"
            placeholder="请输入设备编号"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="el-form-search-item">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
          >
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table
      :height="tableHeight"
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="50" align="center">
        <template scope="scope">
          <span>{{
              (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="name" width="120" />
      <el-table-column label="设备型号" align="center" prop="specification" />
      <el-table-column label="厂家" align="center" prop="factory" width="240"/>
      <el-table-column label="出厂日期" align="factoryDate" prop="factoryDate" width="160" />
      <el-table-column label="公司名称" align="center" prop="companyName" width="180"/>
      <el-table-column label="地址" align="center" prop="address" width="360"/>
      <el-table-column label="公司性质" align="center" prop="companyNature" />
      <el-table-column label="车牌号" align="center" prop="plateNumber"  />
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column
        label="联系方式"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['car:vehicle:edit']"
          >免费获取联系方式</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据小区对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      v-dialogDrag
    >
      <el-link type="warning">所有设备为用户自行上传，请注意辨别真假，防止上当受骗。</el-link>
      <el-divider></el-divider>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="联系人" prop="manager">
          <el-input v-model="form.manager"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="specification">
          <el-input v-model="form.managerPhone"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  del,
  add,
  update,
} from "@/api/transport/car";

export default {
  name: "vehicleList",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据小区表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        used:0,
        pageNum: 1,
        pageSize: 20,
        name: null,
        code: null,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据小区列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        manager: null,
        managerPhone: null,
        plateNumber: null,
        factory: null,
        factoryDate: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      window.open('/cars/car/vehicle');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "温馨提示：";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            update(this.form.id,this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除记录？")
        .then(function () {
          return del(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    /*   handleExport() {
         this.queryParams.title=`vehicle_${new Date().getTime()}.xlsx`;
         exportList(this.queryParams);
       },*/
    handleExport() {
      this.download(
        '/vehicles/export',
        {
          ...this.queryParams,
        },
        `vehicle_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
